#ifndef TOUCH_H
#define TOUCH_H
#include <FT6336U.h>
#include <lvgl.h>
#include <globals.h>
#include <logger.h>


#define TOUCH_SDA 1
#define TOUCH_SCL 2

#define TOUCH_LONG_PUSH_T 200



FT6336U touch_drv(TOUCH_SDA, TOUCH_SCL, PinNotUsed, PinNotUsed); 


void gesture_handler(uint8_t gesture){
    static uint8_t last_gesture = Gestures::None;
    if (last_gesture != gesture){
        if (gesture == Gestures::SlideUp){ // 普通模式时，切换色表。拍照模式时，切换照片
            logln("[TOUCH] Gesture: SlideUp");
        }else if (gesture == Gestures::SlideDown){
            logln("[TOUCH] Gesture: SlideDown");
        }else if(gesture == Gestures::LongPress){
            logln("[TOUCH] Gesture: LongPress");
        }else if (gesture == Gestures::SlideLeft){
            logln("[TOUCH] Gesture: SlideLeft");
        }
        else if (gesture == Gestures::SlideRight){
            logln("[TOUCH] Gesture: SlideRight");
        }
    }
    last_gesture = gesture;
}

void my_touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data )

{
    if(!touch_init_done){return;} // 如果触摸驱动未初始化完成，则不进行读取
    static bool last_touched = false;
    touch_drv.update();
    bool touched = touch_drv.tp.touching;
    if( !touched )
    {
        data->state = LV_INDEV_STATE_REL;
        last_touched = false;
    }
    else
    {
        data->state = LV_INDEV_STATE_PR;
        #if (ROTATE == 0)
        /*Change to your screen resolution*/
        data->point.x = touch_drv.tp.x;
        data->point.y = touch_drv.tp.y;
        #endif
        #if (ROTATE == 1)
        /*Change to your screen resolution*/
        data->point.x = touch_drv.tp.y;
        data->point.y = 240-touch_drv.tp.x;
        gesture_handler(touch_drv.tp.gesture);
        if (TP_LOG){
            if (last_touched == false){            
                logf("[TOUCH] (%d, %d)", data->point.x, data->point.y);
            }
        }
        #endif
        #if (ROTATE == 2)
        /*Change to your screen resolution*/
        data->point.x = 240-touch_drv.tp.x;
        data->point.y = 280-touch_drv.tp.y;
        #endif

        #if (ROTATE == 3)
        data->point.x = 280-touch_drv.tp.y;
        data->point.y = touch_drv.tp.x;
        #endif
        last_touched = true;
    }
}

bool touch_scan(){
    for (uint8_t i = 0; i < 5; i++){
        Wire.begin();
        Wire.beginTransmission(I2C_ADDR_FT6336U);
        uint8_t error = Wire.endTransmission();
        if (error != 0){
            logf("[TOUCH] Tp not found", error);
        }else{
            logln("[TOUCH] Tp found");
            return true;
        } 
        delay(100);
    }
    return false;
}

void touch_setup(void){
    logln("[TOUCH] Setting up");
    touch_drv.begin();

    static lv_indev_drv_t indev_drv;
    lv_indev_drv_init( &indev_drv );
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = my_touchpad_read;
    lv_indev_drv_register( &indev_drv );
    touch_init_done = true;
    logln("[TOUCH] Setting up Done");
}

void touch_setup_nolvgl(void){
    touch_drv.begin();
}

#endif // TOUCH_H